{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "01b01fe1",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sagemaker.serve.model_builder import ModelBuilder\n",
    "from sagemaker.serve.mode.function_pointers import Mode\n",
    "from sagemaker.core.helper.session_helper import Session, get_execution_role\n",
    "from sagemaker.core import image_uris\n",
    "import boto3\n",
    "\n",
    "image_uri = image_uris.retrieve(\n",
    "    framework=\"xgboost\",\n",
    "    region=\"us-east-1\",\n",
    "    version=\"1.0-1\",\n",
    "    py_version=\"py3\",\n",
    "    instance_type=\"ml.m5.xlarge\",\n",
    ")\n",
    "sagemaker_session = Session()\n",
    "role = get_execution_role()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8126089e",
   "metadata": {},
   "outputs": [],
   "source": [
    "s3 = boto3.client('s3')\n",
    "bucket = sagemaker_session.default_bucket()\n",
    "\n",
    "# this is pre-trained xgboost model\n",
    "s3.upload_file('model/model.tar.gz', bucket, 'registry-testing/model.tar.gz')\n",
    "s3_url = f\"s3://{bucket}/registry-testing/model.tar.gz\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8808c5a9",
   "metadata": {},
   "source": [
    "### Build and register a model from model artifact"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff99750e",
   "metadata": {},
   "outputs": [],
   "source": [
    "model_builder = ModelBuilder(\n",
    "    s3_model_data_url=s3_url,\n",
    "    image_uri=image_uri,\n",
    "    sagemaker_session=sagemaker_session,\n",
    "    role_arn=role,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1bb705d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Build the model\n",
    "model_builder.build(model_name=\"my-model\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3581bdda",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Register the model\n",
    "model_builder.register(\n",
    "    model_package_group_name=\"my-model-package-group\",\n",
    "    content_types=[\"application/json\"],\n",
    "    response_types=[\"application/json\"],\n",
    "    inference_instances=[\"ml.m5.xlarge\"],\n",
    "    approval_status=\"Approved\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c220862b",
   "metadata": {},
   "source": [
    "### Register from existing SageMaker Model resource"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c9d62e6",
   "metadata": {},
   "source": [
    "This assumes that you already have a SageMaker Model resource."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e6e4beb8",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sagemaker.core.resources import Model\n",
    "from sagemaker.serve.model_builder import ModelBuilder\n",
    "\n",
    "# your sagemaker model is called my-model\n",
    "model = Model.get(model_name=\"my-model\")\n",
    "\n",
    "# Get image URI and S3 model data URI\n",
    "image_uri = model.primary_container.image\n",
    "s3_model_data_uri = model.primary_container.model_data_url"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "98e6f9aa",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "model_builder = ModelBuilder(\n",
    "    image_uri=image_uri,\n",
    "    s3_model_data_url=s3_model_data_uri,\n",
    "    role_arn=role,\n",
    ")\n",
    "\n",
    "registered_model_package_arn = model_builder.register(\n",
    "    model_package_group_name=\"my-model-group-from-existing-model\",\n",
    "    content_types=[\"application/json\"],\n",
    "    response_types=[\"application/json\"]\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef73c3d8",
   "metadata": {},
   "source": [
    "### Create a SageMaker model from specific registry"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf237383",
   "metadata": {},
   "source": [
    "Here we assume you already have a registered model version in model group, and we create a sagemaker model from this version."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "88f340fd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# approve the version before creating model\n",
    "\n",
    "# There is a gap that API response for a versioned model package doesn't include model_package_name\n",
    "sagemaker_client = boto3.client('sagemaker', region_name='us-east-1')\n",
    "sagemaker_client.update_model_package(\n",
    "    ModelPackageArn=registered_model_package_arn,\n",
    "    ModelApprovalStatus=\"Approved\"\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ef95404d",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# Use the model_package in section above\n",
    "model_builder = ModelBuilder(\n",
    "    model_package_arn=registered_model_package_arn,\n",
    "    role_arn=role,\n",
    "    sagemaker_session=sagemaker_session\n",
    ")\n",
    "\n",
    "\n",
    "\n",
    "# Build the model\n",
    "model = model_builder.build(model_name=\"my-model-from-registry\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
